﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ross.EntityFramework;
using Ross.EntityFramework.Model;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;

namespace Ross.Service
{
    public class CourseClassService : BaseService, IDisposable
    {
        private TranDbContext dbContext;
        public IRepository<CourseClass, int> repository;
        public CourseClassService(string nameOrConnectionString)
        {
            dbContext = new TranDbContext(nameOrConnectionString);
            repository = new Repository<CourseClass, int>(dbContext);
        }
        public void Dispose()
        {
            dbContext.Dispose();
        }
        //添加
        public ResultDto<bool> Create(Dto.CourseClass dto)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            var CourseClass = repository.GetAllList(o =>o.ClassIndex==dto.ClassIndex).Count;
            if (CourseClass >0)
            {
                result.message = "CourseClass is exist";
                result.status = 500;
                result.datas = false;
            }
            else
            {
                try
                {
                    if (dto.ParentID.ToString()=="")
                    {
                        dto.Lev = 1;
                    }
                    else {
                        dto.Lev = dto.ParentID+1;
                    }
                    var obj = repository.Insert(Mapper.Map<CourseClass>(dto));
                    result.datas = true;
                }
                catch (Exception ex)
                {
                    result.message = ex.Message;
                    result.datas = false;
                }
            }
            return result;
        }
        //删除
        public ResultDto<bool> Delete(int id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                repository.Delete(id);
                result.status = 200;
                result.datas = true;
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
        //编辑
        public ResultDto<bool> Update(Dto.CourseClass CClass)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            var obj = repository.GetAllList(o => (o.ClassName==CClass.ClassName||o.ClassIndex==CClass.ClassIndex)&&o.ID!=CClass.ID).FirstOrDefault();
            if (obj == null)
            {

                    CClass.Lev = CClass.ParentID + 1;
                repository.InsertOrUpdate(Mapper.Map<CourseClass>(CClass));
                result.status = 200;
                result.datas = true;
            }
            else
            {
                result.message = "The ClassName or ClassIndex you modified already exists";
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
        //获取列表
        public ResultDto<List<Dto.CourseClass>> GetAllList()
        {
            ResultDto<List<Dto.CourseClass>> result = new ResultDto<List<Dto.CourseClass>>();
            var lists = repository.GetAllList();
            result.datas = Mapper.Map<List<Dto.CourseClass>>(lists);
            return result;
        }

    }
}
